package cn.sangyy.auth.controller;

import cn.sangyy.auth.annotation.Permission;
import cn.sangyy.auth.domain.Permiss;
import cn.sangyy.auth.query.PerQuery;
import cn.sangyy.auth.service.IPermissionService;
import cn.sangyy.basic.utils.AjaxResult;
import cn.sangyy.basic.utils.PageList;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author SangYY
 * @date: 2023/05/17/13:59
 */
@RestController
@RequestMapping("/per")
@Api(tags = "权限管理")
@Permission(name = "权限管理",desc = "管理用户权限")
public class PermissionController {


    @Autowired
    private IPermissionService permissionService;

    /**
    * @author Sangyy
    * 分页加高级查询
    */
    @PostMapping
    @ApiOperation(value = "分页高级查询")
    @Permission(name = "分页+高级查询",desc = "分页显示权限列表")
    public AjaxResult pageList(@RequestBody PerQuery perQuery){
        try {
            PageList<Permiss> pageList = permissionService.pageList(perQuery);
            return AjaxResult.me().setResultObj(pageList);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setCode(201).setMessage(e.getMessage());
        }
    }

    /**
    * @author Sangyy
    * 权限树查询
    */
    @GetMapping
    @ApiOperation(value = "权限树查询")
    @Permission(name = "权限树查询",desc = "树形显示权限列表")
    public AjaxResult getTree(){
        try {
            List<Permiss> list = permissionService.tree();
            return AjaxResult.me().setResultObj(list);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setCode(201).setMessage(e.getMessage());
        }
    }
}
